﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Areas.Admin.Filters
{
    public class AdminLoginFilter : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string actionname = filterContext.ActionDescriptor.ActionName;
            string controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            string area = filterContext.RouteData.DataTokens["area"] == null ? "" : filterContext.RouteData.DataTokens["area"].ToString();

            if (area.Equals("Admin"))
            {
                if (filterContext.HttpContext.Session["Administrator"] == null && !(controller.Equals("Home") && actionname.Equals("Login")))
                {
                    filterContext.Result = new RedirectResult("~/Admin/Home/Login");
                }
            }
        }
    }
}